package com.gem.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gem.back.entity.AoaRole;
import com.gem.back.service.IAoaRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hhhy
 * @since 2020-02-22
 */
@Controller
@RequestMapping("/a/role")
public class AoaRoleController {
    @Autowired
    private IAoaRoleService aoaRoleService;

    @ModelAttribute("role")
    public AoaRole get(Integer roleId){
        if (roleId != null){
            return aoaRoleService.getById(roleId);
        }else {
            return new AoaRole();
        }
    }


    //角色管理
    @RequestMapping("/list")
    public String list(Model model, Integer page, Integer size){
        Integer currentPage = 1;
        Integer pageSize = 5;
        if (page != null){
            currentPage = page;
        }
        if (size != null){
            pageSize = size;
        }
        QueryWrapper<AoaRole> queryWrapper = new QueryWrapper<>();
        IPage<AoaRole> pageList = aoaRoleService.page(new Page<>(currentPage,pageSize),queryWrapper);
        model.addAttribute("roles",pageList.getRecords());
        model.addAttribute("count",pageList.getTotal());
        model.addAttribute("curr",pageList.getCurrent());
        return "role/roleList";
    }

    @RequestMapping("/form")
    public String form(Model model,AoaRole role){
        if (role != null && role.getRoleId() != null){
            role = aoaRoleService.getById(role.getRoleId());
        }
        model.addAttribute("sysRole",role);
        return "role/roleForm";
    }

    @ResponseBody
    @RequestMapping("/save")
    public String save(AoaRole sysRole,int[] roleMenus){
        aoaRoleService.saveRole(sysRole, roleMenus);
        return "success";
    }

    /**
     * 获取角色授权菜单树
     */
    @ResponseBody
    @RequestMapping("/getRoleMenuTree")
    public List<Map<String,Object>> getRoleMenuTree(AoaRole role){
        List<Map<String,Object>> list = aoaRoleService.getRoleMenuTree(role);
        System.out.println(list);
        return list;
    }



}
